package com.bailiban.aigou.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.dao.ProductTypeDao;
import com.bailiban.aigou.entity.ProductType;
import com.bailiban.aigou.utils.PageModel;

@Repository
public class ProductTypeDaoImpl implements ProductTypeDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public PageModel<ProductType> getProductTypes(PageModel<ProductType> pageModel) {
		Session session = sessionFactory.getCurrentSession();
		
		Criteria criteria = session.createCriteria(ProductType.class);
		criteria.setFirstResult((pageModel.getPageNo()-1) * pageModel.getPageSize());
		criteria.setMaxResults(pageModel.getPageSize());
		
		List<ProductType> list = criteria.list();
		pageModel.setList(list);
		return pageModel;
	}

	@Override
	public int getProductTypeCount() {
		Session session = sessionFactory.getCurrentSession();
		
		Query query = session.createQuery("select count(id) from ProductType");
		List<Long> list = query.list();
		return list.get(0).intValue();
	}

	@Override
	public List<ProductType> getAllProductType() {
		Session session = sessionFactory.getCurrentSession();
		
		Query query = session.createQuery("from ProductType");
		return query.list();
	}

	@Override
	public ProductType getProductTypeById(String id) {
		Session session = sessionFactory.getCurrentSession();
		return session.get(ProductType.class, id);
	}

}
